package org.example.strategy;

import java.util.LinkedList;

public class ListTreeStrategy implements EditorStrategy{
    private LinkedList<String> contents;
    private Integer index;

    public ListTreeStrategy(LinkedList<String> contents,Integer index){
        this.contents = contents;
        this.index = index;
    }
    @Override
    public void performAction() {
        if (index < 0){
            displayMarkdownTree(contents,1,0,0);
        }
        else {
            displayMarkdownTree(contents,0,index,1);
        }

    }
    private static int displayMarkdownTree(LinkedList<String> lines, int level, int index,int mode) {
        int i = index;
        if (mode == 0){
            for (; i < lines.size(); i++) {
                String line = lines.get(i).trim();
                // 得到当前行是否是标题 如果是标题是几级标题
                if (line.equals(""))continue;
                int currentLevel = getHeaderLevel(line,level);
                // 判断当前行是否是无序列表或者有序列表
                boolean isUnorderedList = isUnorderedList(line);
                boolean isOrderedList = isOrderedList(line);

                if (currentLevel >= level || isUnorderedList || isOrderedList) {
                    if (currentLevel > level){
                        System.out.println("" + indent(currentLevel) + "└──" + line);
                    }
                    else {
                        System.out.println("" + indent(currentLevel)+ "├──" + line);
                    }
                    // 递归处理下一行
                    i = displayMarkdownTree(lines, currentLevel, i + 1,0)-1;
                } else {
                    return i ;
                }
            }
            return i;
        }
        else {
            for (; i < lines.size(); i++) {
                String line = lines.get(i).trim();
                // 得到当前行是否是标题 如果是标题是几级标题
                if (line.equals(""))continue;
                int currentLevel = getHeaderLevel(line,level);
                // 判断当前行是否是无序列表或者有序列表
                boolean isUnorderedList = isUnorderedList(line);
                boolean isOrderedList = isOrderedList(line);

                if (currentLevel > level || isUnorderedList || isOrderedList) {
                    if (currentLevel > level){
                        System.out.println("" + indent(currentLevel) + "└──" + line);
                        level = currentLevel;
                    }
                    else {
                        System.out.println("" + indent(currentLevel)+ "├──" + line);
                    }
                } else {
                    break;
                }
            }
        }
        return i;
    }
    private static int getHeaderLevel(String line,int lastLineLevel) {
        int level = 0;
        // 是否需要考虑到markdown只有六级标题 并且考虑到标题的 # 和内容之间有空格
        while (level < line.length() && line.charAt(level) == '#' && level < 6) {
            level++;
        }
        if (line.charAt(level) != ' ')return lastLineLevel;
        return level;
    }
    private static boolean isUnorderedList(String line) {
        return line.matches("\\s*[-+]\\s+.*");
    }
    private static boolean isOrderedList(String line) {
        return line.matches("\\s*\\d+\\.\\s+.*");
    }
    private static String indent(int level) {
        StringBuilder indentation = new StringBuilder();
        for (int i = 0; i < level; i++) {
            indentation.append("  "); // 两个空格作为缩进
        }
        return indentation.toString();
    }
}
